package com.fengmi.miaosha.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fengmi.miaosha.entity.MsGoods;
import com.fengmi.miaosha.mapper.MsGoodsMapper;
import com.fengmi.miaosha.service.IMsGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuxm
 * @since 2022-03-17
 */
@Service
public class MsGoodsServiceImpl extends ServiceImpl<MsGoodsMapper, MsGoods> implements IMsGoodsService {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     *
     * @param time :2023071514
     * @return
     */
    @Override
    public List<MsGoods> findGoodsListByTime(String time) {

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps("menhu:miaosha:goods:"+time);

        List<Object> values = operations.values();
        List<MsGoods> msGoods = values.stream().map(item -> JSONUtil.toBean((String) item, MsGoods.class)).collect(Collectors.toList());

        return msGoods;
    }

    @Autowired
    private MsGoodsMapper msGoodsMapper;

    @Override
    public List<MsGoods> findGoodsByTimeFromDB(String startTime, String endTime) {

        QueryWrapper<MsGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().ge(MsGoods::getSeckillStarttime,startTime)
                .le(MsGoods::getSeckillEndtime,endTime)
                .eq(MsGoods::getSeckillStatus,1)
                .gt(MsGoods::getSeckillRemaining,0);

        return msGoodsMapper.selectList(queryWrapper);
    }
}
